//
//  CircleRound.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleRound.h"

@implementation CircleRound
@synthesize _radius = m_Radius, _isDestroy = m_IsDestroy;

-(id) initWithRadius:(float)radius listSegment:(NSArray *)listSegment
{
    if (self = [super init])
    {
        m_ArrSegment = [[NSMutableArray alloc] init];
        m_Radius = radius;
        m_IsDestroy = NO;
        
        for (int i = 1; i < listSegment.count; i++)
        {
            CircleSegment *s = [[CircleSegment alloc] initWithType:[[listSegment objectAtIndex:0] integerValue]
                                                             color:[[listSegment objectAtIndex:i] integerValue]
                                                          rotation:(360  * i / (listSegment.count - 1))];
            [s setRadius:m_Radius];
            [m_ArrSegment addObject:s];
            [s release];
        }
    }
    return self;
}

-(void) addSpriteToBatch:(CCSpriteBatchNode *)batch
{
    for (int i = 0; i < m_ArrSegment.count; i++)
    {
        CircleSegment *s = [m_ArrSegment objectAtIndex:i];
        [s addSpriteToBatch:batch];
    }
}

-(void) appearWithTime:(ccTime)t
{
    CircleSegment *s = [m_ArrSegment objectAtIndex:0];
    [s appearWithTime:t];
}

-(void) setPosition:(CGPoint)p
{
    for (int i = 0; i < m_ArrSegment.count; i++)
    {
        CircleSegment *s = [m_ArrSegment objectAtIndex:i];
        [s setPosition:p];
    }
}

-(void) setRotationSpeed:(float)speed
{
    if (speed == 0) return;
    
    for (int i = 0; i < m_ArrSegment.count; i++)
    {
        CCRotateBy *action = [CCRotateBy actionWithDuration:1.0f angle:speed]; // co the tran so do khong minimize m_Sprite.rotation
        CCRepeatForever *repeat = [CCRepeatForever actionWithAction:action];
        repeat.tag = ACTION_ROTATON_TAG;
        
        CircleSegment *s = [m_ArrSegment objectAtIndex:i];
        [s._sprite runAction:repeat];
    }
}

-(void) stopRotation
{
    for (int i = 0; i < m_ArrSegment.count; i++)
    {
        CircleSegment *s = [m_ArrSegment objectAtIndex:i];
        [s._sprite stopActionByTag:ACTION_ROTATON_TAG];
    }
}

-(BOOL) isCollideBulletWithDistance:(float)d bullet:(Bullet *)b
{
    CircleSegment *s = [m_ArrSegment objectAtIndex:0];
    
    if (d <= m_Radius)
    {
        if (b._type != BULLET_TYPE_NORMAL || s._color == b._color)
        {
            [s explodeWithDelay:0.0f];
            m_IsDestroy = YES;
        }
        return YES;
    }

    return NO;
    
//    if (isCollide == NO)
//        return isCollide;
//    
//    
//    int temp;
//    float newRot;
//    float minAngle;
//    float maxAngle;
//    
//    for (int i = 0; i < m_ArrSegment.count; i++)
//    {
//        CircleSegment *s = [m_ArrSegment objectAtIndex:i];
//        temp = s._sprite.rotation / 360;
//        newRot = s._sprite.rotation - temp * 360;
//        if (newRot < 0)
//            newRot += 360.0f;
//        
//        minAngle = [s getMinAngle] + newRot;
//        maxAngle = [s getMaxAngle] + newRot;
//        if (a >= minAngle && a <= maxAngle)
//        {
//            [s destroy];
//            break;
//        }
//    }
}

-(void) destroyWithDelay:(float)d
{
    // add delay here
    CircleSegment *s = [m_ArrSegment objectAtIndex:0];
    [s explodeWithDelay:d];
    m_IsDestroy = YES;
}

-(void) dealloc
{
    [super dealloc];
    
    [m_ArrSegment removeAllObjects];
    [m_ArrSegment release];
    m_ArrSegment = nil;
}
@end
